import {
	payApi
} from '@/http/payApi';
import store from '@/store';
import {
	mapState
} from "vuex"
export default {
	data() {
		return {
			// #ifdef MP-WEIXIN
			payType: 1,
			// #endif
			// #ifdef APP-PLUS
			payType: 1,
			// #endif
			refresh: true,
			callback: true
		}
	},
	computed: {
		...mapState({
			user_id: state => state.id
		})
	},
	methods: {
		async clientTransferPay(orderNo, flag, orderType) {
			let that = this
			uni.showLoading({
				title: '支付中...',
			})
			try {
				const payRes = await payApi.pay({
					'order_no': orderNo,
					'type': that.payType
				})
				uni.hideLoading()
				if (that.payType == 1) {
					that._wechat_mini_pay(payRes, flag)
				}
				if (that.payType == 2) {
					that.$refs.payPopup.close()
					uni.showLoading({
						title: '支付成功'
					})
				 if (flag) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/subPages/grabOrders/grabOrders?cid=' + that.cid
							})
							uni.hideLoading()
						}, 1500)
					} else if (!flag) {
					 if (orderType == 'assess') {
						 uni.showToast('恭喜您，充值成功！')
					 }else  {
						 setTimeout(() => {
							 uni.navigateTo({
								 url: '/subPages/orderList/orderList'
							 })
							 uni.hideLoading()
						 }, 1500)
					 }
					}
				}

			} catch (error) {
				uni.hideLoading()
				console.log('error', error)
			}
		},
		//小程序支付
		_wechat_mini_pay(paymentData, flag) {
			let that = this
			uni.requestPayment({
				appId: paymentData.appId,
				timeStamp: paymentData.timeStamp,
				nonceStr: paymentData.nonceStr,
				package: paymentData.package,
				signType: paymentData.signType,
				paySign: paymentData.paySign,
				success: (res) => {
					that.$refs.payPopup.close()
					uni.showToast({
						title: '支付成功！'
					})
					if (flag) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/subPages/grabOrders/grabOrders?cid=' + that.cid
							})
							uni.hideLoading()
						}, 1500)
					} else if (!flag) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/subPages/orderList/orderList'
							})
							uni.hideLoading()
						}, 1500)
					}
					if (that.refresh) {
						store.dispatch('refreshUser')
					}
				},
				fail: (err) => {
					uni.showToast({
						title: '支付失败！',
						icon: 'none'
					})
				},
			});
		},
		//app微信支付
		appWxPay(WxpayParams) {
			uni.requestPayment({
				"provider": "wxpay",
				"orderInfo": {
					"appid": WxpayParams.appid,
					"timestamp": WxpayParams.timestamp,
					"noncestr": WxpayParams.noncestr,
					"package": "Sign=WXPay",
					"partnerid": WxpayParams.partnerId,
					"prepayid": WxpayParams.prepayId,
					"sign": WxpayParams.sign
				},
				success: (res) => {

				},
				fail: (err) => {
					console.log('支付失败，错误信息：', err);
					uni.showToast({
						title: '支付失败',
						icon: 'none',
						duration: 2000
					});
				}
			});
		},
		//支付宝支付
		appAliPay(aliData) {
			uni.requestPayment({
				provider: 'alipay',
				orderInfo: aliData,
				success: function(res) {
					// uni.redirectTo({
					// 	url:'/pagesMain/goods/goodsSu',
					// })
				},
				fail: function(err) {
					console.log('err', err)
				}
			});
		},
		//H5支付宝支付
		submitForm(formdata) {
			document.querySelector('body').innerHTML = formdata
			document.forms['0'].submit();
		},
	}
}